﻿using System;
using System.IO;
using AppCommon;
using Controller;
using Model;

public partial class ACPanel_NodeMedia_AddEditLichkhoihanh : System.Web.UI.Page
{
    public static int _id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            var id = Request.QueryString["Id"];
            if (id != null)
            {
                id = id.Replace("'", "''");
                _id = Convert.ToInt32(id);
                BindData(_id);
            }
        }
    }

    public void BindData(int id)
    {
        var controller = new VYC06Controller();
        var model = controller.GetById(id);
        if (model.Count <= 0)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Không tồn tại dữ liệu tương ứng. ";
            return;
        }
        
        tbTieude.Text = model[0].TieuDe;
        // tbTomtat.Text = model[0].TomTat;
        tbNoidung.Text = Server.HtmlDecode(model[0].NoiDung);
        // tbTacgia.Text = model[0].TacGia;
        // tbTags.Text = model[0].Tag;
        // tblienket.Text = model[0].LienKet;
        imgPopup.ImageUrl = Constants.UPLOAD_TINTUC + model[0].AnhDaiDien;
        imgPopup.AlternateText = model[0].AnhDaiDien;
        // cbTrangThai.Checked = Convert.ToBoolean(model[0].TrangThai);
        // cbHienthipanoramanho.Checked = Convert.ToBoolean(model[0].HienthiPanoramanho);
    }

    protected void btnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var model = new ModelVYC06();

            model.IdSubLoaiChuyenMuc = Convert.ToInt32(GetIdLichkhoihanh());
            // get idLoaichuyenmuc
            var ctl = new VYC05Controller();
            var listloaichuyenmuc = ctl.GetById(Convert.ToInt32(GetIdLichkhoihanh()));
            if (listloaichuyenmuc.Count <= 0)
            {
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Chưa chọn chuyên mục con. ";
                return;
            }
            var controller = new VYC06Controller();
            model.IdLoaiChuyenMuc = listloaichuyenmuc[0].IdLoaiChuyenMuc;
            model.TieuDe = tbTieude.Text.Replace("'", "''");
            model.TomTat = "";
            model.NoiDung = Server.HtmlEncode(tbNoidung.Text.Replace("'", "''"));
            model.LienKet = "";
            model.Tag = "";
            if (imgUploadAnhdaidien.HasFile)
            {
                var pathAnhbia = Server.MapPath(Constants.UPLOAD_TINTUC);
                if (!Directory.Exists(pathAnhbia))
                {
                    Directory.CreateDirectory(pathAnhbia);
                }
                // validate file image
                string[] validFileTypes = { "gif", "png", "jpg", "jpeg" };
                var extFile = Path.GetExtension(imgUploadAnhdaidien.PostedFile.FileName);
                var isValidate = false;

                if (extFile != null)
                {
                    foreach (var type in validFileTypes)
                    {
                        if (extFile.Equals("." + type))
                        {
                            isValidate = true;
                            break;
                        }
                    }
                }

                if (!isValidate)
                {
                    litResult.Text = "Invalid file with extension!";
                    model.AnhDaiDien = Constants.NO_IMAGE;
                }
                else
                {

                    litResult.Text = "Ok!";
                    // Luu file 
                    imgUploadAnhdaidien.SaveAs(pathAnhbia + "Baiviet_avatar_" + DateTime.Now.ToString("ddMMyyhhmmss") +
                                    extFile);
                    model.AnhDaiDien = "Baiviet_avatar_" + DateTime.Now.ToString("ddMMyyhhmmss") + extFile;
                }
            }
            else
            {
                model.AnhDaiDien = imgPopup.AlternateText;
                model.AnhDaiDien = Constants.NO_IMAGE;
            }
            
            model.TacGia = "";
            model.UniqueId = Common.ConvertToUnSign(tbTieude.Text.Trim());
            model.TrangThai = 0;
            model.HienthiPanoramanho = 0;
            var isSuccess = 0;
            if (_id != 0)
            {
                model.Id = _id;
                isSuccess = controller.EditData(model);
            }
            else
            {
                isSuccess = controller.AddData(model);
            }

            if (isSuccess == 0)
            {
                // Chua thanh cong
                litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại.</p>";
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-success\">Thêm dữ liệu thành công.</p>";
                ResetInput();
            }

        }
        catch (Exception exc)
        {
            litResult.Text = "<p class=\"alert alert-danger\">Lỗi! Thêm dữ liệu thất bại. " + exc.Message + "</p>";
            throw;
        }
    }
    /// <summary>
    /// Reset all input
    /// </summary>
    public void ResetInput()
    {
        tbTieude.Text = "";
        // tbTomtat.Text = "";
        tbNoidung.Text = "";
        // tblienket.Text = "";
        // tbTags.Text = "";
        _id = 0;
        // cbTrangThai.Checked = false;
    }

    public int GetIdLichkhoihanh()
    {
        var controller = new VYC05Controller();
        var model = controller.GetByUniqueId(Common.ConvertToUnSign(Constants.LKH));
        return model[0].Id;
    }
}